package com.kevinwu.config;

import com.kevinwu.pojo.Configuation;
import com.kevinwu.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {

    private Configuation configuation;

    public XMLMapperBuilder(Configuation configuation) {
        this.configuation = configuation;
    }

    /**
     * 解析业务mapper.xml文件将内容封装mappedStatement
     * @param inputStream
     * @throws DocumentException
     */
    public void parseSelect(InputStream inputStream) throws DocumentException {

        //读取字节流转换成Document对象
        Document document = new SAXReader().read(inputStream);
        //获取根节点
        Element rootElement = document.getRootElement();
        //获取namespace
        String namespace = rootElement.attributeValue("namespace");
        //获取所有的select标签节点
        List<Element> list = rootElement.selectNodes("//select");
        for (Element element : list) {
            //获取id restltType parameterType以及sql 封装成mappedStatment
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setSql(sql);
            String statementId = namespace+"."+id;
            //封装到configuration对象中
            configuation.getMappedStatementMap().put(statementId,mappedStatement);
        }
    }

    /**
     * 解析业务mapper.xml文件将内容封装mappedStatement
     * 支持解析所有的标签 包含select insert update delete
     * @param inputStream
     * @throws DocumentException
     */
    public void parse(InputStream inputStream) throws DocumentException {

        //读取字节流转换成Document对象
        Document document = new SAXReader().read(inputStream);
        //获取根节点
        Element rootElement = document.getRootElement();
        //获取namespace
        String namespace = rootElement.attributeValue("namespace");
        //获取根节点下的所有的节点

        List<Element> list = rootElement.elements();
        for (Element element : list) {
            //获取id restltType parameterType以及sql 封装成mappedStatment
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setSql(sql);
            String statementId = namespace+"."+id;
            //封装到configuration对象中
            configuation.getMappedStatementMap().put(statementId,mappedStatement);
        }
    }
}
